Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(index): support adding index widget with initial UI state #4359

Merged
merged 16 commits into from
May 13, 2020

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Mar 18, 2020

Sorry for this larger pull request, if it proves too complicated for a single PR, we can split this up in three smaller PRs

  1. when hydrating (server or client) we add widgets, these shouldn't search yet, until started

since every added widget calls scheduleSearch if init has happened, this used to do a search, even before start was called, and thus with not yet the correct state

  1. make sure local ui state keeps "not yet mounted" widgets on change

e.g. initialUiState contains a few widgets, if you mount those one by one, after init on its index, each widget would cause a change event on the helper, which used to erase non-valid state, since it starts from an empty ui state. This is now fixed by using the previous local ui state as base, but a special provision for isPageReset

  1. prevent mutation in escapeHits

It's okay to do results.hits = ... inside connectHits, since that overrides just the value on SearchResults, however if we mutate within the inner hits, we are actually mutating _rawResults, which isn't what we want to achieve for server side rendering, since _rawResults gets serialised.

Further changes are just minor for tests

How to test this?

Two options:

  1. the tests themselves show a use case of uiState which shouldn't be removed
  2. feat(server side rendering): support ais-index vue-instantsearch#764 has instructions

Haroenv added a commit to algolia/vue-instantsearch that referenced this pull request Mar 18, 2020
Haroenv added 3 commits April 24, 2020 13:55
in Vue InstantSearch server side rendering we call .addWidgets in hydrate, and otherwise this would cause an extra search. If we do keep this search then uiState which gets modified in the index widget needs to be changed.
src/widgets/index/index.ts Outdated Show resolved Hide resolved
src/widgets/index/index.ts Outdated Show resolved Hide resolved
src/widgets/index/index.ts Outdated Show resolved Hide resolved
@Haroenv Haroenv changed the title WIP: fixes for server side rendering fix(server side rendering): allow a started index to have ui state for a not-yet-mounted widget, prevent searches before start, and prevent mutation on escapeHits May 4, 2020
@Haroenv Haroenv marked this pull request as ready for review May 4, 2020 14:56
@Haroenv
Copy link
Contributor Author

Haroenv commented May 5, 2020

E2E test failure seems to be a fluke (cc @yannickcr), locally the URL updates as expected in those scenarios

src/lib/escape-highlight.ts Outdated Show resolved Hide resolved
src/lib/escape-highlight.ts Outdated Show resolved Hide resolved
Co-authored-by: Nuno Maduro <enunomaduro@gmail.com>
Copy link
Contributor

@eunjae-lee eunjae-lee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me. The PR is not too big.
Let me know if you need me to test anything or take a closer look at any part.

@Haroenv
Copy link
Contributor Author

Haroenv commented May 5, 2020

The e2e test seems to actually catch a bug; if a refinement is set again to the default value of the widget (usually nothing) the previous value for uiState is kept by the widget. This is expected on first render, but not later.

@Haroenv
Copy link
Contributor Author

Haroenv commented May 5, 2020

The problem is related to this line

https://github.com/algolia/instantsearch.js/blob/9ff71e2341acc5b09133e095c3285a2de162b846/src/widgets/index/index.ts#L418-L425

Before this pull request, every change to the helper, will erase the uiState which is "still there", since it was reduced from an empty object. This was causing issues in Vue InstantSearch, since every widget added on server side erased the initialUiState (except the first widget to be mounted).

The solution I saw was to base the new state of the existing state. The issue that pops up because of this is that when a widget refines, and later goes back to its default / unrefined state, all widgets have a guard that check "if not refined, return uiState", and thus they keep the previously refined state in the URL & uiState.

A change I can imagine is in all widgets themselves removing that condition and returning a uiState with explicitly undefined for those values, but that likely would break intialUiState & routing.

Copy link
Contributor

@eunjae-lee eunjae-lee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canceling approval until the bug is fixed.

src/widgets/index/index.ts Show resolved Hide resolved
src/widgets/index/index.ts Outdated Show resolved Hide resolved
src/widgets/index/index.ts Outdated Show resolved Hide resolved
src/widgets/index/index.ts Show resolved Hide resolved
src/widgets/index/index.ts Show resolved Hide resolved
{
state,
isPageReset,
_isFromAddWidget,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(1) Instead of saying where it's from, what about saying what it does? (similarly to isPageReset). This name looses sense suddenly if we were to reuse it somewhere else. A suggestion: _shouldInheritUiState.

(2) To make it more testable and easier to think about, I would rather pass the baseUiState object here, instead of a boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, I've changed it to _uiState, where we pass the local ui state in this case

src/widgets/index/index.ts Show resolved Hide resolved
src/widgets/index/index.ts Show resolved Hide resolved
src/widgets/index/index.ts Outdated Show resolved Hide resolved
src/widgets/index/index.ts Outdated Show resolved Hide resolved
@Haroenv Haroenv changed the title fix(server side rendering): allow a started index to have ui state for a not-yet-mounted widget, prevent searches before start, and prevent mutation on escapeHits fix(index): support adding index widget with initial UI state May 13, 2020
@Haroenv Haroenv merged commit 5ff4c83 into master May 13, 2020
@Haroenv Haroenv deleted the feat/ssr-fixes branch May 13, 2020 13:43
Haroenv added a commit to algolia/vue-instantsearch that referenced this pull request Jun 9, 2020
* feat(ssr): support multi index

* WIP: update nuxt example with new API

* different option

* Revert "different option"

This reverts commit f37fa7e.

* remove debug leak

* hydrate main index & use in force render

* don't init in force render, because addWidget does it alreayd

* fixes, ref: algolia/instantsearch#4359

* outdated note

* initial next version of ssr

todo: tests

* more realistic instantsearch integration test

* update widget mixin tests based on main index change

* move stuff slightly

* consistency in test

* initial server root tests

* commit the patch for now

* commit patch

* fix: wip

* have a more real test

* update tests

* well discussed test cases & small tweaks

* use test utility

* actualize bundlesize

* update examples

* clean up example

* remove lint error

* suppress another lint

* more patches for demo

* commit paches

* make dumb lint error disappear

* fix flaky jest tests

* test

* update deps

* ecom example fix

* Update src/util/createServerRootMixin.js

Co-authored-by: Eunjae Lee <eunjae.lee@algolia.com>

* chore(deps): update e2e tests (#784)

* chore(deps): update e2e tests

* modern node

* ci(circle): update node

* ci(examples): use working version of core-js

* chore(netlify): remove node modules

* chore(ssr): avoid JSON cost

* chore(ssr): don't resolve unused things

* clean todos

* fix typo

* change comments

Co-authored-by: Vaillant Samuel <samuel.vllnt@gmail.com>
Co-authored-by: Eunjae Lee <eunjae.lee@algolia.com>
Haroenv added a commit to algolia/vue-instantsearch that referenced this pull request Jun 11, 2020
* feat(ssr): support multi index

* WIP: update nuxt example with new API

* different option

* Revert "different option"

This reverts commit f37fa7e.

* remove debug leak

* hydrate main index & use in force render

* don't init in force render, because addWidget does it alreayd

* fixes, ref: algolia/instantsearch#4359

* outdated note

* initial next version of ssr

todo: tests

* more realistic instantsearch integration test

* update widget mixin tests based on main index change

* move stuff slightly

* consistency in test

* initial server root tests

* commit the patch for now

* commit patch

* fix: wip

* have a more real test

* update tests

* well discussed test cases & small tweaks

* use test utility

* actualize bundlesize

* update examples

* clean up example

* remove lint error

* suppress another lint

* more patches for demo

* commit paches

* make dumb lint error disappear

* fix flaky jest tests

* test

* update deps

* ecom example fix

* Update src/util/createServerRootMixin.js

Co-authored-by: Eunjae Lee <eunjae.lee@algolia.com>

* chore(deps): update e2e tests (#784)

* chore(deps): update e2e tests

* modern node

* ci(circle): update node

* ci(examples): use working version of core-js

* chore(netlify): remove node modules

* chore(ssr): avoid JSON cost

* chore(ssr): don't resolve unused things

* clean todos

* fix typo

* change comments

Co-authored-by: Vaillant Samuel <samuel.vllnt@gmail.com>
Co-authored-by: Eunjae Lee <eunjae.lee@algolia.com>
Haroenv added a commit that referenced this pull request Dec 28, 2022
…arch#764)

* feat(ssr): support multi index

* WIP: update nuxt example with new API

* different option

* Revert "different option"

This reverts commit f37fa7e626be09af6c31fe3cf4bd310967319aef.

* remove debug leak

* hydrate main index & use in force render

* don't init in force render, because addWidget does it alreayd

* fixes, ref: #4359

* outdated note

* initial next version of ssr

todo: tests

* more realistic instantsearch integration test

* update widget mixin tests based on main index change

* move stuff slightly

* consistency in test

* initial server root tests

* commit the patch for now

* commit patch

* fix: wip

* have a more real test

* update tests

* well discussed test cases & small tweaks

* use test utility

* actualize bundlesize

* update examples

* clean up example

* remove lint error

* suppress another lint

* more patches for demo

* commit paches

* make dumb lint error disappear

* fix flaky jest tests

* test

* update deps

* ecom example fix

* Update src/util/createServerRootMixin.js

Co-authored-by: Eunjae Lee <eunjae.lee@algolia.com>

* chore(deps): update e2e tests (algolia/vue-instantsearch#784)

* chore(deps): update e2e tests

* modern node

* ci(circle): update node

* ci(examples): use working version of core-js

* chore(netlify): remove node modules

* chore(ssr): avoid JSON cost

* chore(ssr): don't resolve unused things

* clean todos

* fix typo

* change comments

Co-authored-by: Vaillant Samuel <samuel.vllnt@gmail.com>
Co-authored-by: Eunjae Lee <eunjae.lee@algolia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants